#version 460 core
#include <flutter/runtime_effect.glsl>
precision mediump float;// 设置默认精度

out vec4 fragColor;//外传参数
uniform vec2 iResolution;//屏幕分辨率，flutter中可以指画布尺寸
uniform vec2 iMouse;//点击位置
uniform float boardSizeF;//棋盘路数
uniform float iTime;//动画进度 [0-1]
//uniform float heatMap[361];//热点图数据， 数组不支持动态索引，只支持constant
uniform sampler2D iTextureHeatMap;
//uniform sampler2D iTexture;
//uniform sampler2D iTextureBlack;

//双线性插值，已知线性变换的正方形4个顶点，求内部任意一点的值
float bilinearInterpolation(
float x, float y, float leftTop, float rightTop, float leftBottom, float rightBottom) {
    float result = leftTop * (1 - x) * (1 - y) +
    rightTop * x * (1 - y) +
    leftBottom * (1 - x) * y +
    rightBottom * x * y;
    return result;
}

//获取通过纹理传过来的大数据
float getValue(vec2 xy) {
    return (texture(iTextureHeatMap, xy).r * 255 - 100) / 100;
}

void main() {

    vec2 uv = FlutterFragCoord().xy / iResolution.xy;
    //    // 获取纹理颜色
    //    vec4 textureColor = texture(iTexture, uv);
    //    vec3 colorBg = textureColor.rgb;//绘制图片
    //绘制棋盘，暂时只固定一个背景色
    vec4 colorBg = vec4(1.0, 0.5, 0.0, 1.0);
    vec4 color = colorBg;

    int boardSize = int(boardSizeF);

    //绘制指定位置形状  圆
    float tileSize = 1.0/(boardSize + 1);
    float maxRadius = tileSize * 0.8;
    float radius = (maxRadius - tileSize / 2) * iTime + tileSize / 2;
    float radiusDiffusion = tileSize * 0.13 * clamp(iTime, 0.5, 1.0);
    vec2 mouseCenter = iMouse / iResolution;
    float distanceToMouse = distance(uv, mouseCenter);
    vec4 circleColor = vec4(0.0, 0.0, 0.0, 1.0);

    //热点图
    if (uv.x > tileSize / 2 && uv.x < 1 -  tileSize / 2 && uv.y > tileSize / 2 && uv.y < 1 -  tileSize / 2) {
        //位于第几行几列的格子
        vec2 xy = uv - tileSize / 2;
        int x = int(clamp(xy.x / tileSize, 0, boardSize-1));// 1 - boardSize
        int y = int(clamp(xy.y / tileSize, 0, boardSize-1));// 1 - boardSize

        //该格子的四个顶点值
        float leftTop = 0.0;
        float rightTop = 0.0;
        float leftBottom = 0.0;
        float rightBottom = 0.0;
        if (x >= 1 && y >= 1) {
            leftTop = getValue(vec2((x - 1) / (boardSize-1.0), (y - 1)/(boardSize-1.0)));
        }
        if (x < boardSize - 1 && y >= 1) {
            rightTop = getValue(vec2((x + 1) / (boardSize-1.0), (y - 1)/(boardSize-1.0)));
        }
        if (x >= 1 && y <  boardSize - 1) {
            leftBottom = getValue(vec2((x - 1) / (boardSize-1.0), (y + 1)/(boardSize-1.0)));
        }
        if (x < boardSize - 1 && y <  boardSize - 1) {
            rightBottom = getValue(vec2((x + 1) / (boardSize-1.0), (y + 1)/(boardSize-1.0)));
        }

        //当前坐标位于该格子的映射
        vec2 p = fract(xy/tileSize) / tileSize;

        //计算当前坐标的近似值
        float value = bilinearInterpolation(p.x, p.y, leftTop, rightTop, leftBottom, rightBottom);

        //某个格子内的点到格子中心的距离
        vec2 blockCenter = vec2(x, y) * tileSize + tileSize/2;
        float distanceToBlockCenterX = distance(xy.x, blockCenter.x);
        float distanceToBlockCenterY = distance(xy.y, blockCenter.y);

        //测试
        float test = getValue(vec2(x / (boardSize-1.0), y/(boardSize-1.0)));

        //绘图
        if (distanceToBlockCenterX < tileSize / 2 && distanceToBlockCenterY < tileSize / 2) {
            vec4 blockColor;
            if(test < 0) {
                blockColor = vec4(0.0, 0.0, 0.0, abs(test));
            } else {
                blockColor = vec4(1.0, 1.0, 1.0, abs(test));
            }
            color = mix(colorBg,blockColor, abs(test));
        }
    }

    //预落子动画
    if (distanceToMouse > radius - radiusDiffusion && distanceToMouse < radius + radiusDiffusion && iTime < 1.0 && iMouse.x > 0) {
        //t [0..1]
        float t = smoothstep(radius - radiusDiffusion, radius + radiusDiffusion, distance(uv, mouseCenter));
        //t [0..1..0]
        t = 1.0 - sin(t * 3.14);
        t = clamp(t, iTime, 1.0);
        color = t *  colorBg;
    }



    fragColor = color;
}
